package com.xm.linxiaogang.network.bean

import com.alibaba.fastjson.serializer.SerializeConfig
import com.xm.linxiaogang.network.converter.FastJsonRequestBodyConverter
import com.xm.linxiaogang.network.converter.FastJsonResponseBodyConverter
import okhttp3.RequestBody
import okhttp3.ResponseBody
import retrofit2.Converter
import retrofit2.Retrofit
import java.lang.reflect.Type

class FastJsonConverterFactory private constructor(serializeConfig: SerializeConfig) :
    Converter.Factory() {

    private val serializeConfig: SerializeConfig

    init {
        this.serializeConfig = serializeConfig
    }

    /**
     * 重写responseBodyConverter，该方法用来解析服务器返回的body数据
     */
    override fun responseBodyConverter(
        type: Type,
        annotations: Array<Annotation>,
        retrofit: Retrofit
    ): Converter<ResponseBody, *> {
        return FastJsonResponseBodyConverter<ResponseBody>(type)
    }

    /**
     * 重写responseBodyConverter，该方法用来转换发送给服务器的body数据
     */
    override fun requestBodyConverter(
        type: Type,
        parameterAnnotations: Array<Annotation>,
        methodAnnotations: Array<Annotation>,
        retrofit: Retrofit
    ): Converter<*, RequestBody> {
        return FastJsonRequestBodyConverter<RequestBody>(serializeConfig)
    }

    companion object {
        fun create(): FastJsonConverterFactory {
            return FastJsonConverterFactory(SerializeConfig.getGlobalInstance())
        }

        fun create(serializeConfig: SerializeConfig): FastJsonConverterFactory {
            return FastJsonConverterFactory(serializeConfig)
        }
    }


}